<template>
	<view class="download">
		<view class="file-info-box">
			<image class="file-img" src="../../static/xubao/excel.png"></image>
			<view class="file-name">{{fileName ? fileName : "-"}}</view>
			<view class="file-desc">已导出为本地文件</view>
		</view>

		<view class="file-btns">
			<view class="share-btn" @click.stop="shareFile">分享好友</view>
			<view class="preview-btn" @click.stop="previewFile">预览文件</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageType: '',
				fileName: "",
				filePath: "",
			}
		},
		onLoad(options) {
			if (options) {
				if (options.type == 'product') {
					this.pageType = 'product';
					this.fileName = '自定义产品配置模板.xlsx';
					this.downloadProductTemplate('product');
				} else if (options.type == 'xubao') {
					this.pageType = 'xubao';
					this.fileName = '续保导入模板.xlsx';
					this.downloadProductTemplate('xubao');
				} else if (options.type == 'dingbao') {
					this.pageType = 'dingbao';
					this.fileName = '定保导入模板.xlsx';
					this.downloadProductTemplate('dingbao');
				} else if (options.type == 'dingbaoDataExport') {
					this.pageType = 'dingbaoDataExport';
					this.fileName = '定保维系数据.xlsx';
					this.downloadWxData('定保');
				} else if (options.type == 'xubaoDataExport') {
					this.pageType = 'xubaoDataExport';
					this.fileName = '续保维系数据.xlsx';
					this.downloadWxData('续保');
				}
			}
		},
		methods: {
			// 下载文件
			downloadProductTemplate(type) {
				var that = this;
				getApp().showLoading();
				let temId = -1;
				if (type == 'product') {
					temId = 1002;
				} else if (type == 'xubao') {
					temId = 1004;
				} else if (type == 'dingbao') {
					temId = 1003;
				}
				uni.downloadFile({
					url: 'https://biapi.vaubang.cn/api/v1/FileDownload/Template?id=' + temId,
					success: (res) => {
						uni.hideLoading();
						if (res.statusCode === 200) {
							let filePath = res.tempFilePath;
							console.log('下载结果', res);
							that.filePath = filePath;
						} else {
							getApp().showToast(res.errMsg);
						}
					},
					fail(err) {
						uni.hideLoading();
						getApp().showToast(err);
					}
				});
			},
			// 下载定保/续保维系数据
			downloadWxData(type) {
				const loginInfo = uni.getStorageSync("loginInfo");
				var that = this;
				getApp().showLoading();
				uni.downloadFile({
					url: 'https://biapi.vaubang.cn/api/v1/Keep/Task/Export?type=' + type,
					header: {
						'Authorization': ('Bearer ' + loginInfo.token) || ''
					},
					success: (res) => {
						uni.hideLoading();
						if (res.statusCode === 200) {
							let filePath = res.tempFilePath;
							console.log('下载结果', res);
							that.filePath = filePath;
						} else {
							getApp().showToast(res.errMsg);
						}
					},
					fail(err) {
						uni.hideLoading();
						getApp().showToast(err);
					}
				});
			},
			// 分享文件
			shareFile() {
				var that = this;
				wx.shareFileMessage({
					filePath: that.filePath,
					fileName: that.fileName,
					success(res) {
						console.log('成功打开分享');
					}
				})
			},
			// 预览文件
			previewFile() {
				var that = this;
				uni.openDocument({
					filePath: that.filePath,
					fileType: "xlsx",
					showMenu: true,
					success(res) {
						console.log('打开文档成功');
					}
				})
			},
		},
	}
</script>

<style lang="less">
	.download {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		.file-info-box {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			margin-top: 100px;

			.file-img {
				width: 80px;
				height: 80px;
			}

			.file-name {
				font-size: 16px;
				color: #000;
				font-weight: 500;
				margin-top: 30px
			}

			.file-desc {
				font-size: 13px;
				color: #999;
				margin-top: 5px;
			}
		}

		.file-btns {
			margin-top: 60px;

			.share-btn {
				width: 200px;
				height: 40px;
				line-height: 40px;
				background-color: #008aff;
				color: #fff;
				border-radius: 4px;
				font-weight: 500;
				font-size: 14px;
				text-align: center;

				&:active {
					background-color: #0076DB;
				}
			}

			.preview-btn {
				width: 200px;
				height: 40px;
				line-height: 40px;
				background-color: #E9EAEC;
				color: #000;
				border-radius: 4px;
				font-weight: 500;
				margin-top: 15px;
				text-align: center;
				font-size: 14px;

				&:active {
					background-color: #ddd;
				}
			}
		}
	}
</style>
